package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.system.domain.TPatient;
import com.ruoyi.system.domain.vo.TVisitorVo;
import com.ruoyi.system.service.ITPatientService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TVisitor;
import com.ruoyi.system.service.ITVisitorService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 访问登记Controller
 * 
 * @author 杨一迪
 * @date 2024-12-28
 */
@Controller
@RequestMapping("/system/visitor")
public class TVisitorController extends BaseController
{
    private String prefix = "system/visitor";

    @Resource
    private ITPatientService patientService;

    @Autowired
    private ITVisitorService tVisitorService;

    @RequiresPermissions("system:visitor:view")
    @GetMapping()
    public String visitor()
    {
        return prefix + "/visitor";
    }

    /**
     * 查询访问登记列表
     */
    @RequiresPermissions("system:visitor:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TVisitor tVisitor)
    {
        startPage();
        List<TVisitor> list = tVisitorService.selectTVisitorList(tVisitor);
        List<TVisitorVo> visitorVos = new ArrayList<>();
        list.forEach(visitor -> {
            TVisitorVo tVisitorVo = new TVisitorVo();
            BeanUtils.copyProperties(visitor,tVisitorVo);
            TPatient tPatient = patientService.selectTPatientById(visitor.getPatientId());
            tVisitorVo.setPatientName(tPatient.getName());
            visitorVos.add(tVisitorVo);
        });
        return getDataTable(visitorVos);
    }

    /**
     * 导出访问登记列表
     */
    @RequiresPermissions("system:visitor:export")
    @Log(title = "访问登记", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TVisitor tVisitor)
    {
        List<TVisitor> list = tVisitorService.selectTVisitorList(tVisitor);
        ExcelUtil<TVisitor> util = new ExcelUtil<TVisitor>(TVisitor.class);
        return util.exportExcel(list, "访问登记数据");
    }

    /**
     * 新增访问登记
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        List<TPatient> tPatients = patientService.selectTPatientList(new TPatient());
        mmap.put("tPatients",tPatients);
        return prefix + "/add";
    }

    /**
     * 新增保存访问登记
     */
    @RequiresPermissions("system:visitor:add")
    @Log(title = "访问登记", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TVisitor tVisitor)
    {
        return toAjax(tVisitorService.insertTVisitor(tVisitor));
    }

    /**
     * 修改访问登记
     */
    @RequiresPermissions("system:visitor:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TVisitor tVisitor = tVisitorService.selectTVisitorById(id);
        mmap.put("tVisitor", tVisitor);
        List<TPatient> tPatients = patientService.selectTPatientList(new TPatient());
        mmap.put("tPatients",tPatients);
        return prefix + "/edit";
    }

    /**
     * 修改保存访问登记
     */
    @RequiresPermissions("system:visitor:edit")
    @Log(title = "访问登记", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TVisitor tVisitor)
    {
        return toAjax(tVisitorService.updateTVisitor(tVisitor));
    }

    /**
     * 删除访问登记
     */
    @RequiresPermissions("system:visitor:remove")
    @Log(title = "访问登记", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tVisitorService.softDeleteTVisitorByIds(ids));
    }
}
